package com.diankangwo.dao.impl;

import com.diankangwo.dao.RefundMoneyDAO;
import com.diankangwo.domain.RefundMoney;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


/**
 * 退费接口实现类
 *
 * @author xiaoyu
 */
@Repository
public class RefundMoneyDAOImpl extends BaseDaoImpl<RefundMoney> implements RefundMoneyDAO {

    /**
     * 查询所有退费记录
     */
    @Override
    public List<RefundMoney> findAll() {
        List<RefundMoney> refunMoneyList = new ArrayList<RefundMoney>();//创建退费记录集合
        refunMoneyList = findAll(RefundMoney.class);//得到所有退费记录
        return refunMoneyList;
    }

    /**
     * 按条件查询退费记录
     */
    @Override
    public List<RefundMoney> findByCondition(RefundMoney re) {
        List<RefundMoney> refunMoneyList = new ArrayList<RefundMoney>();//创建退费记录集合，存储查询结果
        //拼接hql语句
        StringBuffer hql = new StringBuffer();
        hql.append("from refundMoney where 1=1 ");
        if (re.getState() != null) {
            hql.append("and state = " + re.getState());
        }
        if (re.getName() != null) {
            hql.append("and Name = " + re.getName());
        }
        if (re.getExamineFlow() != null) {
            hql.append("and examineFlow = " + re.getExamineFlow());
        }
        //得到查询结果
        refunMoneyList = findByQuery(hql.toString());
        return refunMoneyList;
    }

    /**
     * 增加、更新退费记录
     */
    @Override
    public boolean insertOrUpdateRefunMoney(RefundMoney re) {
        boolean isOk;//标识是否新增成功
        try {
            saveOrUpdate(re);//执行新增操作
            isOk = true;//新增、更新成功
        } catch (Exception e) {
            //打印异常信息
            e.printStackTrace();
            //出现异常说明新增、更新失败
            isOk = false;
        }
        return isOk;
    }

    /**
     * 删除退费记录
     */
    @Override
    public boolean deleteRefunMoney(RefundMoney re) {
        boolean isOk;//标识是否删除成功
        try {
            delete(re);//执行新增操作
            isOk = true;//删除成功
        } catch (Exception e) {
            //打印异常信息
            e.printStackTrace();
            //出现异常说明删除失败
            isOk = false;
        }
        return isOk;
    }

    /**
     * 按ID删除退费记录
     */
    @Override
    public boolean deleteRefunMoneyById(int id) {
        boolean isOk;//标识是否删除成功
        try {
            deleteById(id);//执行新增操作
            isOk = true;//删除成功
        } catch (Exception e) {
            //打印异常信息
            e.printStackTrace();
            //出现异常说明删除失败
            isOk = false;
        }
        return isOk;
    }

    @Override
    public RefundMoney findByOrderNo(String orderNo) {
        List<RefundMoney> list = findByQuery("from RefundMoney where PaymenOrderNo = ?", orderNo);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }

    }

    /**
     * 按id查询信息
     */
    @Override
    public RefundMoney findById(int id) {
        return find(id);
    }

}
